//admin upload
$(function(){
	upload.run();
});

var upload = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || "";
		},
		
		_start_: function(){						
		},
		
		_init_events: function(){
			this.newfiledialog = $("#new_file_div").dialog({				
			});
			
			$("#new_file_div > div.dialog-content").tabs();
			
			$("#root_content_div > div.root-dir > div.dir-box > a.unfold").click(function(){
				upload._bind_fold_unfold_events($(this));
			});
			
			$("#root_content_div > div.root-dir > div > a.delete").click(function(){
				upload._bind_delete_events($(this));
			});
			
			$("#root_content_div > div.root-dir > a.add").click(function(){
				upload._bind_add_files($(this));
			});			
			
			$("#root_content_div > div.root-dir > div.dir-box > a.add").click(function(){
				upload._bind_add_files($(this));
			});
			
			this._bind_add_file_or_dir_event();
		},
		
		_bind_fold_unfold_events: function($a){	
			var $sibling = $a.siblings(".child-box");
			var action = $a.attr("action");
			if(action == 'unfold'){	
				if($a.data("_get_")){
					$sibling.show();
					$a.attr("action", "");
					$a.text("收起");
				}else{
					var dir = $a.attr("dir");
					var url = upload.contextPath + "/admin/website/upload/unfold?dir=" + encodeURIComponent(dir);
					$.get(url, function(result){
						$a.data("_get_", true);
						$a.attr("action", "");
						$a.text("收起");
						$sibling.html(result);				
						$sibling.children("div.dir-box").children("a.unfold").click(function(){						
							upload._bind_fold_unfold_events($(this));
						});
						
						$sibling.children("div").children("a.delete").click(function(){						
							upload._bind_delete_events($(this));
						});
						
						$sibling.children("div").children("a.add").click(function(){						
							upload._bind_add_files($(this));
						});
					});
				}					
			}else{
				$sibling.hide();
				$a.attr("action", "unfold");
				$a.text("展开");
			}
		},
		
		_bind_delete_events: function($a){
			var file = $a.attr("file");
			var isdir = $a.attr("isdir");
			var tip = "file: ["+ file + "]";
			if(isdir){
				tip = "directory: [" + file + "], all file in this directory will also be deleted!";
			}
			var cc = confirm("Are you sure to delete " + tip);
			if(cc){				
				var url = upload.contextPath + "/admin/website/upload/delete";
				var data = "dir=" + encodeURIComponent(file);
				$.post(url, data, function(result){				
					var aff = parseInt(result);
					if(aff > 0){
						$a.parent().remove();
					}
				});
			}			
		},
		
		_bind_add_file_or_dir_event: function(){
			$("#create_dir_btn").click(function(){
				var dirname = $("#dirname").val();
				if(dirname){
					var $a = upload.$uploada;
					var p = $a.attr("dir");
					var url = upload.contextPath + "/admin/website/upload/mkdir";
					var data = "dir=" + encodeURIComponent(dirname);
					data += "&parent=" + encodeURIComponent(p);
					$.post(url, data, function(result){
						$("#dirname").val("");
						upload.newfiledialog.dialog("close");
						if(p == '/'){
							location.reload(true);
						}
					});
				}
			});
			
			$("#upload_file_btn").click(function(){
				var $files = $("#upload_file_btn").siblings(":file");
				var p = encodeURIComponent(upload.$uploada.attr("dir"));
				$files.ajaxupload({
					url: upload.contextPath + "/admin/website/upload/files?parent=" + p,
					success: function(data){
						upload.newfiledialog.dialog("close");
					}
				});
			});
		},
		
		_bind_add_files: function($a){
			this.$uploada = $a;
			var dir = $a.attr("dir");
			this.newfiledialog.dialog("open");
			$("#new_file_div > div.dialog-title").html("当前文件操作相对于http://www.wontube.com/images/ext" + dir);
		}
};